from rest_framework import serializers

from polls.models import Subject, Teacher
from polls.utils import format_date


class TeacherCreateSerializer(serializers.ModelSerializer):

    class Meta:
        model = Teacher
        fields = '__all__'


class TeacherSerializer(serializers.ModelSerializer):
    sex = serializers.SerializerMethodField()
    birth = serializers.SerializerMethodField()
    subject = serializers.SerializerMethodField()

    @staticmethod
    def get_sex(teacher):
        return '男' if teacher.sex else '女'

    @staticmethod
    def get_birth(teacher):
        return format_date(teacher.birth)

    @staticmethod
    def get_subject(teacher):
        return SubjectSimpleSerializer(teacher.subject).data

    class Meta:
        model = Teacher
        exclude = ()


class SubjectSimpleSerializer(serializers.ModelSerializer):

    class Meta:
        model = Subject
        fields = ('no', 'name')


class SubjectSerializer(serializers.ModelSerializer):

    class Meta:
        model = Subject
        exclude = ('create_date', )
